package com.sbtr.controller.common;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.extension.service.IService;
import com.google.common.base.CaseFormat;
import com.sbtr.common.Response;
import com.sbtr.exception.BDException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通用controller，特殊处理用重写即可
 *
 * @author dgl
 * @since 2023-03-17
 */
public class CommonController<T extends Model<T>,U extends IService<T>>{


    @Autowired
    public U u;

    @GetMapping("/list")
    @ApiOperation(httpMethod = "GET", value = "通用接口---列表")
    public Response<List<T>> list(T t){
        Wrapper<T> wrapper =  new QueryWrapper<>(t);
        return Response.success(t.selectList(wrapper));
    }

    @GetMapping("/getById")
    @ApiOperation(httpMethod = "GET", value = "通用接口---根据id查询")
    public Response<T> getById(Long id){
        return Response.success(u.getById(id));
    }

    @PostMapping("/update")
    @ApiOperation(httpMethod = "POST", value = "通用接口---修改")
    public Response<String> update(@RequestBody T t){
        if(!t.updateById()){
            throw new BDException("修改失败");
        }
        return Response.success("修改成功");
    }

    @PostMapping("/updateList")
    @ApiOperation(httpMethod = "POST", value = "通用接口---根据数据的id批量修改")
    public Response<String> updateList(@RequestBody List<T> t){
        if(!u.updateBatchById(t)){
            throw new BDException("修改失败");
        }
        return Response.success("修改成功");
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation(httpMethod = "POST", value = "通用接口---根据数据的id修改或不存在id就添加")
    public Response<T> saveOrUpdate(@RequestBody T t){
        if(!u.saveOrUpdate(t)){
            throw new BDException("执行失败");
        }
        return Response.success(t);
    }

    @PostMapping("/saveOrUpdateBatch")
    @ApiOperation(httpMethod = "POST", value = "通用接口---根据数据的id批量修改或不存在id就批量添加")
    public Response<List<T>> saveOrUpdateBatch(@RequestBody List<T> t){
        if(!u.saveOrUpdateBatch(t)){
            throw new BDException("执行失败");
        }
        return Response.success(t);
    }

    @PostMapping("/saveOrUpdateBatchBfClear")
    @ApiOperation(httpMethod = "POST", value = "通用接口---先根据key，value删除数据，然后根据数据的id批量修改或不存在id就批量添加")
    public Response<List<T>> saveOrUpdateBatchBfClear(@RequestBody List<T> t, @RequestParam("key") String key,@RequestParam("value") String value){
        if(StringUtils.isBlank(key)){
            throw new BDException("请输入key");
        }

        //驼峰转换成下换线
        String realKey=CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE,key);
        Map<String,Object> param = new HashMap<>();
        param.put(realKey,value);
        u.removeByMap(param);
        if(!u.saveOrUpdateBatch(t)){
            throw new BDException("执行失败");
        }
        return Response.success(t);
    }

    @PostMapping("/deleteByIds")
    @ApiOperation(httpMethod = "POST", value = "通用接口---根据id批量删除,逗号分隔ps:'1,2,3'，关联子表删除请重写")
    public Response<String> deleteByIds(String ids){
        List<String> list = Arrays.asList(ids.split(","));
        if(!u.removeByIds(list)){
            throw new BDException("删除失败");
        }
        return Response.success("删除成功");
    }

}
